package com.ming.rabbitmq.producer.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 通配符模式-发送消息
 */
public class Main {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("119.91.238.104");
        factory.setPort(5672);
        factory.setVirtualHost("/topic");
        factory.setUsername("admin");
        factory.setPassword("admin");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String exchangeName = "topic";
        // 创建交换机
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true,false,false,null);
        // 创建队列
        String queue1_name = "topic_queue1";
        String queue2_name = "topic_queue2";

        // 绑定队列和交换机
        // 队列名称,交换机名称,路由键
        channel.queueBind(queue1_name,exchangeName,"#.error");
        channel.queueBind(queue1_name,exchangeName,"order.*");
        channel.queueBind(queue2_name,exchangeName,"*.*");

        String message = "topic log...";
        channel.basicPublish(exchangeName,"goods.error",null,message.getBytes());

        channel.close();
        connection.close();


    }
}
